<template>
  <el-button
    @click.native.prevent="openDialog"
    :type="styleType"
    :size="size"
    :delApi="delApi"
    v-has="btnper"
    :disabled="disabled"
    :loading="loading"
    :btnUrl="btnUrl"
    :handleParam="handleParam"
  >
    {{ text }}
    <i :class="icon"></i>
  </el-button>
</template>
<script>
import { FmlTools } from "@/utils";
export default {
  name: "fmlAddEditButton",
  data() {
    return {
      btnper: {},
      loading: false
    };
  },
  props: {
    handleParam: Object, //操作按钮上的参数
    text: String, //按钮名称
    btnUrl: String, //按钮权限
    resetPassApi: Function, //重置密码接口
    delApi: Function, //删除接口
    type: {
      type: String,
      default: "add" // add  edit resetPass
    },
    isDialogCopy: {
      type: Number,
      default: 1
    },
    disabled: {
      type: Boolean,
      default: false
    },
    styleType: {
      type: String,
      default: "text"
    },
    size: {
      type: String,
      default: "small"
    },
    icon: String,
    editConfig: Object,
    scope: Object // table的scope对象
  },

  methods: {
    // 重置密码
    async resetPassword(param) {
      let [err] = await this.resetPassApi(param);
      if (err) return this.$message({message:err.message || "重置密码失败",type:'error',showClose:true});
      this.$message({ type: "success", message: "重置成功",showClose:true });
      if (param.userId == this.$store.state.userInfo.userId) {
        localStorage.removeItem("userInfo");
        this.$store.commit("resetUser");
        this.$router.replace({ path: "/login" });
      } else {
        this.$store.commit("updateTable");
      }
    },
    // 删除
    async delHandle(param) {
      const [err] = await this.delApi(this.handleParam);
      if (err) {
        return this.$message({message:err.message || "操作失败",showClose:true,type:'error'});
      }
      this.$message({
        type: "success",
        message: "删除成功",
        showClose:true
      });
      // 删除重新渲染列表
      if (this.isDialogCopy && Number(this.isDialogCopy) == 2) {
        this.$store.commit("updateTableDialog");
      } else {
        this.$store.commit("updateTable");
      }
    },
    // 弹窗
    openDialog(e) {
      window.event ? (window.event.cancelBubble = true) : e.stopPropagation();
      // 修改 新增 审核
      if (this.type === "edit" || this.type === "add"||this.type=='audit'||this.type=='look'||this.type=='newEdit') {
        if (this.editConfig.isDataFromScope&&(this.type=="edit"||this.type=="audit"||this.type=="look"||this.type=='newEdit')) {
          this.$store.commit("setEditData", this.scope.row);
          this.$store.commit("setDialogType", this.type);
        }else{
          this.$store.state.rowData=this.scope.row//在当前行上新增，需要拿到当前行的某些元素
        }
        if (this.isDialogCopy == 2) {
          this.$store.commit("setDialogVisibleCopy", true);
        } else {
          this.$store.commit("setDialogVisible", true);
        }
      }
      // 删除
      if (this.type == "del") {
        if (this.handleParam) {
          FmlTools.confirmTips(this,{
              title: "提示",
              message: "确定删除当前数据吗？删除后将无法恢复"
            }, btn => {
              if (btn == "confirm") {
                this.delHandle(this.handleParam);
              }
            }
          );
        }
      }
      // 重置密码
      if (this.type === "resetPass") {
        if (this.handleParam) {
          FmlTools.confirmTips(this,{
              title: "提示",
              message: "确认重置密码吗？"
            },btn => {
              if (btn == "confirm") {
                this.resetPassword(this.handleParam);
              }
            }
          );
        }
      }
    }
  },
  created() {
    this.btnper = { url: this.$route.path, btnUrl: this.btnUrl };
  }
};
</script>


